3 * Rebuild recent changes from scratch. This takes several hours,
4 * depending on the database size and server configuration.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
22 * @ingroup Maintenance
26 require_once __DIR__
. '/Maintenance.php';
27 use MediaWiki\MediaWikiServices
;
30 * Maintenance script that rebuilds recent changes from scratch.
32 * @ingroup Maintenance
34 class RebuildRecentchanges
extends Maintenance
{
35 /** @var int UNIX timestamp */
37 /** @var int UNIX timestamp */
40 public function __construct() {
41 parent
::__construct();
42 $this->addDescription( 'Rebuild recent changes' );
46 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
52 "Only rebuild rows in requested time range (in YYYYMMDDHHMMSS format)",
56 $this->setBatchSize( 200 );
59 public function execute() {
61 ( $this->hasOption( 'from' ) && !$this->hasOption( 'to' ) ) ||
62 ( !$this->hasOption( 'from' ) && $this->hasOption( 'to' ) )
64 $this->error( "Both 'from' and 'to' must be given, or neither", 1 );
67 $this->rebuildRecentChangesTablePass1();
68 $this->rebuildRecentChangesTablePass2();
69 $this->rebuildRecentChangesTablePass3();
70 $this->rebuildRecentChangesTablePass4();
71 $this->rebuildRecentChangesTablePass5();
72 if ( !( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) ) {
75 $this->output( "Done.\n" );
79 * Rebuild pass 1: Insert `recentchanges` entries for page revisions.
81 private function rebuildRecentChangesTablePass1() {
82 $dbw = $this->getDB( DB_MASTER
);
84 if ( $this->hasOption( 'from' ) && $this->hasOption( 'to' ) ) {
85 $this->cutoffFrom
= wfTimestamp( TS_UNIX
, $this->getOption( 'from' ) );
86 $this->cutoffTo
= wfTimestamp( TS_UNIX
, $this->getOption( 'to' ) );
88 $sec = $this->cutoffTo
- $this->cutoffFrom
;
89 $days = $sec / 24 / 3600;
90 $this->output( "Rebuilding range of $sec seconds ($days days)\n" );
94 $days = $wgRCMaxAge / 24 / 3600;
95 $this->output( "Rebuilding \$wgRCMaxAge=$wgRCMaxAge seconds ($days days)\n" );
97 $this->cutoffFrom
= time() - $wgRCMaxAge;
98 $this->cutoffTo
= time();
101 $this->output( "Clearing recentchanges table for time range...\n" );
102 $rcids = $dbw->selectFieldValues(
106 'rc_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
107 'rc_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
110 foreach ( array_chunk( $rcids, $this->mBatchSize
) as $rcidBatch ) {
111 $dbw->delete( 'recentchanges', [ 'rc_id' => $rcidBatch ], __METHOD__
);
112 wfGetLBFactory()->waitForReplication();
115 $this->output( "Loading from page and revision tables...\n" );
117 [ 'page', 'revision' ],
132 'rev_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
133 'rev_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
137 [ 'ORDER BY' => 'rev_timestamp DESC' ]
140 $this->output( "Inserting from page and revision tables...\n" );
142 foreach ( $res as $row ) {
146 'rc_timestamp' => $row->rev_timestamp
,
147 'rc_user' => $row->rev_user
,
148 'rc_user_text' => $row->rev_user_text
,
149 'rc_namespace' => $row->page_namespace
,
150 'rc_title' => $row->page_title
,
151 'rc_comment' => $row->rev_comment
,
152 'rc_minor' => $row->rev_minor_edit
,
154 'rc_new' => $row->page_is_new
,
155 'rc_cur_id' => $row->page_id
,
156 'rc_this_oldid' => $row->rev_id
,
157 'rc_last_oldid' => 0, // is this ok?
158 'rc_type' => $row->page_is_new ? RC_NEW
: RC_EDIT
,
159 'rc_source' => $row->page_is_new ? RecentChange
::SRC_NEW
: RecentChange
::SRC_EDIT
161 'rc_deleted' => $row->rev_deleted
165 if ( ( ++
$inserted %
$this->mBatchSize
) == 0 ) {
166 wfGetLBFactory()->waitForReplication();
172 * Rebuild pass 2: Enhance entries for page revisions with references to the previous revision
173 * (rc_last_oldid, rc_new etc.) and size differences (rc_old_len, rc_new_len).
175 private function rebuildRecentChangesTablePass2() {
176 $dbw = $this->getDB( DB_MASTER
);
178 $this->output( "Updating links and size differences...\n" );
180 # Fill in the rc_last_oldid field, which points to the previous edit
183 [ 'rc_cur_id', 'rc_this_oldid', 'rc_timestamp' ],
185 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
186 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
189 [ 'ORDER BY' => 'rc_cur_id,rc_timestamp' ]
196 foreach ( $res as $obj ) {
199 if ( $obj->rc_cur_id
!= $lastCurId ) {
200 # Switch! Look up the previous last edit, if any
201 $lastCurId = intval( $obj->rc_cur_id
);
202 $emit = $obj->rc_timestamp
;
204 $row = $dbw->selectRow(
206 [ 'rev_id', 'rev_len' ],
207 [ 'rev_page' => $lastCurId, "rev_timestamp < " . $dbw->addQuotes( $emit ) ],
209 [ 'ORDER BY' => 'rev_timestamp DESC' ]
212 $lastOldId = intval( $row->rev_id
);
213 # Grab the last text size if available
214 $lastSize = !is_null( $row->rev_len
) ?
intval( $row->rev_len
) : null;
219 $new = 1; // probably true
223 if ( $lastCurId == 0 ) {
224 $this->output( "Uhhh, something wrong? No curid\n" );
226 # Grab the entry's text size
227 $size = (int)$dbw->selectField(
230 [ 'rev_id' => $obj->rc_this_oldid
],
237 'rc_last_oldid' => $lastOldId,
239 'rc_type' => $new ? RC_NEW
: RC_EDIT
,
240 'rc_source' => $new === 1 ? RecentChange
::SRC_NEW
: RecentChange
::SRC_EDIT
,
241 'rc_old_len' => $lastSize,
242 'rc_new_len' => $size,
245 'rc_cur_id' => $lastCurId,
246 'rc_this_oldid' => $obj->rc_this_oldid
,
247 'rc_timestamp' => $obj->rc_timestamp
// index usage
252 $lastOldId = intval( $obj->rc_this_oldid
);
255 if ( ( ++
$updated %
$this->mBatchSize
) == 0 ) {
256 wfGetLBFactory()->waitForReplication();
263 * Rebuild pass 3: Insert `recentchanges` entries for action logs.
265 private function rebuildRecentChangesTablePass3() {
266 global $wgLogTypes, $wgLogRestrictions;
268 $dbw = $this->getDB( DB_MASTER
);
270 $this->output( "Loading from user, page, and logging tables...\n" );
273 [ 'user', 'logging', 'page' ],
289 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
290 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
292 // Some logs don't go in RC since they are private.
293 // @FIXME: core/extensions also have spammy logs that don't go in RC.
294 'log_type' => array_diff( $wgLogTypes, array_keys( $wgLogRestrictions ) ),
297 [ 'ORDER BY' => 'log_timestamp DESC' ],
300 [ 'LEFT JOIN', [ 'log_namespace=page_namespace', 'log_title=page_title' ] ]
304 $field = $dbw->fieldInfo( 'recentchanges', 'rc_cur_id' );
307 foreach ( $res as $row ) {
311 'rc_timestamp' => $row->log_timestamp
,
312 'rc_user' => $row->log_user
,
313 'rc_user_text' => $row->user_name
,
314 'rc_namespace' => $row->log_namespace
,
315 'rc_title' => $row->log_title
,
316 'rc_comment' => $row->log_comment
,
321 'rc_this_oldid' => 0,
322 'rc_last_oldid' => 0,
324 'rc_source' => RecentChange
::SRC_LOG
,
325 'rc_cur_id' => $field->isNullable()
327 : (int)$row->page_id
, // NULL => 0,
328 'rc_log_type' => $row->log_type
,
329 'rc_log_action' => $row->log_action
,
330 'rc_logid' => $row->log_id
,
331 'rc_params' => $row->log_params
,
332 'rc_deleted' => $row->log_deleted
337 if ( ( ++
$inserted %
$this->mBatchSize
) == 0 ) {
338 wfGetLBFactory()->waitForReplication();
344 * Rebuild pass 4: Mark bot and autopatrolled entries.
346 private function rebuildRecentChangesTablePass4() {
347 global $wgUseRCPatrol, $wgMiserMode;
349 $dbw = $this->getDB( DB_MASTER
);
351 list( $recentchanges, $usergroups, $user ) =
352 $dbw->tableNamesN( 'recentchanges', 'user_groups', 'user' );
354 # @FIXME: recognize other bot account groups (not the same as users with 'bot' rights)
355 # @NOTE: users with 'bot' rights choose when edits are bot edits or not. That information
356 # may be lost at this point (aside from joining on the patrol log table entries).
357 $botgroups = [ 'bot' ];
358 $autopatrolgroups = $wgUseRCPatrol ? User
::getGroupsWithPermission( 'autopatrol' ) : [];
360 # Flag our recent bot edits
362 $botwhere = $dbw->makeList( $botgroups );
364 $this->output( "Flagging bot account edits...\n" );
366 # Find all users that are bots
367 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
368 "WHERE ug_group IN($botwhere) AND user_id = ug_user";
369 $res = $dbw->query( $sql, __METHOD__
);
372 foreach ( $res as $obj ) {
373 $botusers[] = $obj->user_name
;
376 # Fill in the rc_bot field
378 $rcids = $dbw->selectFieldValues(
382 'rc_user_text' => $botusers,
383 "rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
384 "rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) )
389 foreach ( array_chunk( $rcids, $this->mBatchSize
) as $rcidBatch ) {
393 [ 'rc_id' => $rcidBatch ],
396 wfGetLBFactory()->waitForReplication();
401 # Flag our recent autopatrolled edits
402 if ( !$wgMiserMode && $autopatrolgroups ) {
403 $patrolwhere = $dbw->makeList( $autopatrolgroups );
406 $this->output( "Flagging auto-patrolled edits...\n" );
408 # Find all users in RC with autopatrol rights
409 $sql = "SELECT DISTINCT user_name FROM $usergroups, $user " .
410 "WHERE ug_group IN($patrolwhere) AND user_id = ug_user";
411 $res = $dbw->query( $sql, __METHOD__
);
413 foreach ( $res as $obj ) {
414 $patrolusers[] = $dbw->addQuotes( $obj->user_name
);
417 # Fill in the rc_patrolled field
418 if ( $patrolusers ) {
419 $patrolwhere = implode( ',', $patrolusers );
420 $sql2 = "UPDATE $recentchanges SET rc_patrolled=1 " .
421 "WHERE rc_user_text IN($patrolwhere) " .
422 "AND rc_timestamp > " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ) . ' ' .
423 "AND rc_timestamp < " . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) );
424 $dbw->query( $sql2 );
430 * Rebuild pass 5: Delete duplicate entries where we generate both a page revision and a log entry
431 * for a single action (upload only, at the moment, but potentially also move, protect, ...).
433 private function rebuildRecentChangesTablePass5() {
434 $dbw = wfGetDB( DB_MASTER
);
436 $this->output( "Removing duplicate revision and logging entries...\n" );
439 [ 'logging', 'log_search' ],
440 [ 'ls_value', 'ls_log_id' ],
442 'ls_log_id = log_id',
443 'ls_field' => 'associated_rev_id',
444 'log_type' => 'upload',
445 'log_timestamp > ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffFrom
) ),
446 'log_timestamp < ' . $dbw->addQuotes( $dbw->timestamp( $this->cutoffTo
) ),
452 foreach ( $res as $obj ) {
453 $rev_id = $obj->ls_value
;
454 $log_id = $obj->ls_log_id
;
456 // Mark the logging row as having an associated rev id
459 /*SET*/ [ 'rc_this_oldid' => $rev_id ],
460 /*WHERE*/ [ 'rc_logid' => $log_id ],
464 // Delete the revision row
467 /*WHERE*/ [ 'rc_this_oldid' => $rev_id, 'rc_logid' => 0 ],
471 if ( ( ++
$updates %
$this->mBatchSize
) == 0 ) {
472 wfGetLBFactory()->waitForReplication();
478 * Purge cached feeds in $wanCache
480 private function purgeFeeds() {
481 global $wgFeedClasses;
483 $this->output( "Deleting feed timestamps.\n" );
485 $wanCache = MediaWikiServices
::getInstance()->getMainWANObjectCache();
486 foreach ( $wgFeedClasses as $feed => $className ) {
487 $wanCache->delete( $wanCache->makeKey( 'rcfeed', $feed, 'timestamp' ) ); # Good enough for now.
492 $maintClass = "RebuildRecentchanges";
493 require_once RUN_MAINTENANCE_IF_MAIN
;